var _ioService=Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
var ATOMIC_PLUGIN_NS = "http://www.atomojo.org/Vocabulary/Atomic/Plugin/2007/1/0";


function Plugin() {
   this.id = null;
   this.owner = null;
   this.entryEditors = {};
   this.entryToolbarItems = {};
}

var _Plugin_NAMESPACE = "http://www.atomojo.org/Vocabulary/Atomic/Plugin/2007/1/0";

Plugin.prototype.load = function(manifest) {
   this.id = manifest.getAttribute("id");
   consoleService.logStringMessage("Loading plugin "+this.id);
   var current = this;
   var baseURI = _ioService.newURI(manifest.baseURI,null,null);
   this.owner = _Atom_textOf(manifest,"owner",_Plugin_NAMESPACE);
   consoleService.logStringMessage("Plugin owner: "+this.owner);
   _Atom_forChild(manifest,"entry-editor",_Plugin_NAMESPACE,function(target) {
      var editor = {
         id: target.getAttribute("id"),
         label: _Atom_textOf(target,"label",_Plugin_NAMESPACE),
         description: _Atom_textOf(target,"description",_Plugin_NAMESPACE)
      }
      _Atom_forChild(target,"editor",_Plugin_NAMESPACE,function(link) {
         var url = _ioService.newURI(link.getAttribute("href"),null,baseURI).spec;
         editor["xul"] = url;
      });
      _Atom_forChild(target,"icon",_Plugin_NAMESPACE,function(link) {
         var url = _ioService.newURI(link.getAttribute("src"),null,baseURI).spec;
         editor["icon"] = url;
      });
      current.entryEditors[editor.id] = editor;
   });
   _Atom_forChild(manifest,"toolbar",_Plugin_NAMESPACE,function(toolbar) {
      if (toolbar.getAttribute("id")!="entry-toolbar") {
         consoleService.logStringMessage("Unsupported toolbar "+toolbar.getAttribute("id"));
         return;
      }
      _Atom_forChild(toolbar,"entry-editor",_Plugin_NAMESPACE,function(link) {
         var id = link.getAttribute("ref");
         current.entryToolbarItems[id] = current.entryEditors[id];
      });
   });
   _Atom_forChild(manifest,"action",_Plugin_NAMESPACE,function(target) {
      if (target.getAttribute("type")!="edit") {
         return;
      }
      if (target.getAttribute("target")!="entry") {
         return;
      }
      _Atom_forChild(target,"association",_Plugin_NAMESPACE,function(link) {
         var id = link.getAttribute("to");
         var editor = current.entryEditors[id];
         var matcher = new EntryMatcher();
         _Atom_forChild(link,"category",ATOM_NS,function(category) {
            var scheme = link.getAttribute("scheme");
            var term = link.getAttribute("term");
            matcher.categories[matcher.categories.length] = {
               scheme: scheme,
               term: term
            };
         });
      });
   });
   consoleService.logStringMessage("Finished "+this.id);
}

function EntryMatcher() {
   this.categories = [];
   this.editor = null;
}

EntryMatcher.prototype.match = function(entry) {
   var matches = [];
   for (var i=0; i<this.categories.length; i++) {
      matches[i] = false;
   }
   var current = this;
   _Atom_forChild(entry.xml,"category",ATOM_NS,function(c) {
      var scheme = link.getAttribute("scheme");
      var term = link.getAttribute("term");
      for (var i=0; i<current.categories.length; i++) {
         if (current.categories[i].term==term && current.categories[i].scheme==scheme) {
            matches[i] = true;
         }
      }
   });
   for (var i=0; i<this.categories.length; i++) {
      if (!matches[i]) {
         return false;
      }
   }
   return true;
}

